package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.service.WxPayService;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.consts.PayTypeConst;
import com.ibiaoke.customer.dao.XcxOpenGroupDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.XcxOpenGroup;
import com.ibiaoke.order.dao.XcxOrderDao;
import com.ibiaoke.order.entity.XcxOrder;
import com.ibiaoke.order.service.OrderPayService;
import com.ibiaoke.order.service.XcxOrderService;
import com.ibiaoke.order.service.XcxPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Map;

@Controller
public class XcxPayController extends BaseController {

    @Autowired
    private XcxOrderDao xcxOrderDao;

    @Autowired
    private XcxOpenGroupDao xcxOpenGroupDao;

    @Autowired
    private XcxPayOrderService xcxPayOrderService;

    @Autowired
    private XcxOrderService xcxOrderService;

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private OrderPayService orderPayService;


    /**
     * 小程序付款
     */
    @RequestMapping("/xcxPay")
    @ResponseBody
    public Res payForPtGoodsOpen(Integer xcxOrderId, Integer paymentType, Double payMoney, String smsCode) {
        XcxOrder xcxOrder = xcxOrderDao.selectByPrimaryKey(xcxOrderId);
        //再一次校验，拼团商品是否还有空位
        if(xcxOrder.getIsopen()!=null){
            xcxOrderService.checkBeforePay(xcxOrder);
        }
        //更改XcxOrder的金额和支付类型
        xcxOrder.setOrderPrice(new BigDecimal(payMoney));
        xcxOrder.setPayType(paymentType);
        xcxOrderDao.updateByPrimaryKeySelective(xcxOrder);
        //更改XcxOpenGroupOrder的金额和支付类型
        if(Integer.valueOf(xcxOrder.getOrderType())==1 || Integer.valueOf(xcxOrder.getOrderType())==2){
            XcxOpenGroup xcxOpenGroup = xcxOpenGroupDao.selectByPrimaryKey(xcxOrder.getGroupId());
            xcxOpenGroup.setPayMoneny(new BigDecimal(payMoney));
            xcxOpenGroup.setPayType(paymentType);
            xcxOpenGroupDao.updateByPrimaryKeySelective(xcxOpenGroup);
        }
        String openId = (String) getSession().getAttribute("xcxOpenid");
        //微信支付
        if (paymentType == 1) {
            Map<String, String> payInfo = xcxPayOrderService.payXcxOrderByWe(xcxOrderId, openId);
            return Res.create().addResponse("payInfo", payInfo);
        }
        //预存款支付
        if (paymentType == 4) {
            xcxPayOrderService.payXcxOrderByDepositPay(xcxOrder, smsCode);
            return Res.create();
        }
        return Res.create();
    }

    /**
     * 小程序快捷支付
     */
    @RequestMapping("/xcxOffLinePay")
    @ResponseBody
    public Res offLinePay(Integer shopId, BigDecimal cash, Integer payType, String smsCode) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        String openId = (String) getSession().getAttribute("xcxOpenid");
        if (payType == PayTypeConst.WECAHT) {
            Map<String, String> payInfo = wxPayService.xcxOffLinePay(shopId, cash, customer, openId);
            return Res.create().addResponse("payInfo", payInfo);
        }else if (payType == PayTypeConst.xcx_DEPOSIT) {
            orderPayService.xcxOffLinePayByDeposit(shopId, cash, customer, smsCode);
            return Res.create();
        }
        return Res.create();
    }

    /**
     * 小程序退款
     */
    @RequestMapping("/xcxRefund")
    @ResponseBody
    public Res XcxRefund(String orderCode,BigDecimal orderPrice){
        //将订单编号转成字符串
        XcxOrder xcxOrder = xcxOrderDao.getOrderByOrderCode(orderCode);

        if(xcxOrder.getOrderStatus()== 7){
            throw new ServiceException("订单已退款，请勿重复退款");
        }

        //如果退款金额小于等于订单金额退款
        if (orderPrice.compareTo(xcxOrder.getOrderPrice())==0) {
                //如果支付方式为1（微信支付）
                if (xcxOrder.getPayType() == 1) {
                    //调用service中的XcxPtPayRefundByManual的方法
                    xcxOrderService.XcxPtPayRefundByManual(xcxOrder);
                }
                //如果支付方式为4（账户余额）
                else if (xcxOrder.getPayType() == 4) {
                    //调用service中的XcxPtPayRefundByManual的方法
                    xcxPayOrderService.refundPayByDeposit(xcxOrder);
                }
            } else {
            throw new ServiceException("输入的退款金额不能大于订单实际支付金额");
        }
        return Res.create();
    }
}
